#ifndef SDK_OHOS_OHOS_PEER_CONNECTION_FACTORY_H_
#define SDK_OHOS_OHOS_PEER_CONNECTION_FACTORY_H_

#include "api/peer_connection_interface.h"
#include "rtc_base/thread.h"
#include "api/ohos_peer_connection_factory_interface.h"
#include "api/ohos_peer_connection_interface.h"

namespace ohoswebrtc {

class OHOSPeerConnectionFactory : public OHOSPeerConnectionFactoryInterface {
  public:
    OHOSPeerConnectionFactory();  
    virtual ~OHOSPeerConnectionFactory();
    static rtc::scoped_refptr<OHOSPeerConnectionFactory> Create();

    /** Initialize an RTCPeerConnection with a configuration, constraints. */
    rtc::scoped_refptr<OHOSPeerConnectionInterface> CreatePeerConnection(
      webrtc::PeerConnectionInterface::RTCConfiguration& configuration,
      rtc::scoped_refptr<OHOSMediaConstraintsInterface> constraints,
      OHOSPeerConnectionObserver *observer) override;

    /** Returns the capabilities of an RTP sender of type `kind`. */
    webrtc::RtpCapabilities GetRtpSenderCapabilities(
      cricket::MediaType kind) const override;

    /** Returns the capabilities of an RTP receiver of type `kind`. */
    webrtc::RtpCapabilities GetRtpReceiverCapabilities(
      cricket::MediaType kind) const override;

    /** Initialize an OHOSAudioSource with constraints. */
    rtc::scoped_refptr<OHOSAudioSourceInterface> CreateAudioSource(
      rtc::scoped_refptr<OHOSMediaConstraintsInterface> constraints) override;

    /** Initialize an OHOSAudioTrack with an id. Convenience ctor to use an audio source
     * with no constraints.
     */
    rtc::scoped_refptr<OHOSAudioTrackInterface> CreateAudioTrack(
      rtc::scoped_refptr<OHOSAudioSourceInterface> source, const std::string track_id) override;

    /** Initialize a generic OHOSVideoSource with he posibility of marking
     * it as usable for screen sharing. The OHOSVideoSource should be
     * passed to a CameraVideoCapturer, in order to produce frames.
     */
    rtc::scoped_refptr<OHOSVideoSourceInterface> CreateVideoSource(const bool is_screen) override;

    /** Initialize an OHOSVideoTrack with a source and an id. */
    rtc::scoped_refptr<OHOSVideoTrackInterface> CreateVideoTrack(
      rtc::scoped_refptr<OHOSVideoSourceInterface> source, const std::string track_id) override;
    
    /** Initialize an OHOSMediaStream with an id. */
    rtc::scoped_refptr<OHOSMediaStreamInterface> CreateLocalMediaStream(
      const std::string stream_id) override;

    /**
     * PeerConnectionFactoryInterface representation of this OHOSPeerConnectionFactory
     */
    rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> peer_connection_factory() override {
      return peer_connection_factory_;
    }
    
    rtc::Thread* network_thread() override { return network_thread_.get(); }

    rtc::Thread* signaling_thread() override { return signaling_thread_.get(); }

    rtc::Thread* worker_thread() override { return worker_thread_.get(); }

  protected:
    void CreateAudioDeviceModule_s();

    rtc::scoped_refptr<OHOSPeerConnectionInterface> CreatePeerConnection_s(
      webrtc::PeerConnectionInterface::RTCConfiguration& configuration,
      rtc::scoped_refptr<OHOSMediaConstraintsInterface> constraints,
      OHOSPeerConnectionObserver *observer);
  
    rtc::scoped_refptr<OHOSAudioSourceInterface> CreateAudioSource_s(
      rtc::scoped_refptr<OHOSMediaConstraintsInterface> constraints);

    rtc::scoped_refptr<OHOSAudioTrackInterface> CreateAudioTrack_s(
      rtc::scoped_refptr<OHOSAudioSourceInterface> source, const std::string track_id);

    rtc::scoped_refptr<OHOSVideoSourceInterface> CreateVideoSource_s(const bool is_screen_cast);

    rtc::scoped_refptr<OHOSVideoTrackInterface> CreateVideoTrack_s(
      rtc::scoped_refptr<OHOSVideoSourceInterface> source, const std::string track_id);

    rtc::scoped_refptr<OHOSMediaStreamInterface> CreateLocalMediaStream_s(
      const std::string stream_id);

  private:
    std::unique_ptr<rtc::Thread> worker_thread_;
    std::unique_ptr<rtc::Thread> signaling_thread_;
    std::unique_ptr<rtc::Thread> network_thread_;
    std::unique_ptr<webrtc::TaskQueueFactory> task_queue_factory_;
    rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> peer_connection_factory_;
    rtc::scoped_refptr<webrtc::AudioDeviceModule> audio_device_module_;
};

}

#endif